package com.ruoyi.vote.common.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.exception.vote.VoteException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.redis.RedisUtils;
import com.ruoyi.vote.admin.domain.VoteActivity;
import com.ruoyi.vote.common.constants.VoteConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author 立勤
 * @date 2023/07/17 11:12:36
 */
@Service
@RequiredArgsConstructor
public class VoteCheckUtils {

    /**
     * 活动状态校验
     *
     * @param eventKey   活动Key
     */
    public static VoteActivity activityCheck(String eventKey) {
        VoteActivity activity = RedisUtils.getCacheObject(eventKey);
        // 校验活动是否存在
        if(ObjectUtil.isEmpty(activity)){
            throw new VoteException("vote.activity.doesNotExist",eventKey);
        }
        // 校验活动状态停用
        if (Objects.equals(activity.getStatus(), VoteConstants.VOTE_STATUS_ERROR)){
            throw new VoteException("vote.activity.toClose",eventKey);
        }
        // 活动是否结束
        if (DateUtil.compare(activity.getBalanceTime(), DateUtils.getNowDate()) == -1){
            throw new VoteException("vote.activity.ended",eventKey);
        }
        return activity;
    }

    /**
     * 选手校验
     * @param contestantKey   选手key
     */
    public static void memberCheck(String contestantKey){
        // 选手是否参与投票
        if (!RedisUtils.isExistsObject(contestantKey)){
            throw new VoteException("vote.activity.user.doesNotExist",contestantKey);
        }
    }

}



